﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;

namespace MoviePlayer
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Element_LostFocus(object sender, RoutedEventArgs e)
        {
            object objectInFocus = System.Windows.Input.FocusManager.GetFocusedElement();

            if (!IsRelated(objectInFocus, sender))
            {
                myMovieControl.Pause();
                Debug.WriteLine("Movie Paused by MainPage");
            }
        }

        private void Element_GotFocus(object sender, RoutedEventArgs e)
        {
            String name = "not a control";
            if (sender is Control)
            {
                name = ((Control)sender).Name;
            }
            Debug.WriteLine(sender.GetType().FullName + ": " + name + " is in Focus");
        }

        private bool IsRelated(object child, object parent)
        {
            return (child == parent) || ((child is FrameworkElement) && IsRelated(((FrameworkElement)child).Parent, parent));
        }

    }
}
